package com.nwcl.ysx.controller;

import com.nwcl.basic.bo.OrdersBO;
import com.nwcl.basic.bo.OrdersDetailBO;
import com.nwcl.basic.service.OrdersDetailService;
import com.nwcl.basic.service.OrdersService;
import com.nwcl.ysx.vo.OrdersGoodsVO;
import com.nwcl.ysx.vo.OrdersVO;
import com.nwcl.common.constant.Constant;
import com.nwcl.common.result.Page;
import com.nwcl.common.result.Result;
import com.nwcl.common.utils.DateUtil;
import com.nwcl.common.utils.SessionUtil;
import com.nwcl.ysx.request.OrdersPageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/ysx/orders")
public class ApiOrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrdersDetailService ordersDetailService;
    @Autowired
    private SessionUtil sessionUtil;

    /*
     * @Description:分页查询我的订单列表
     * @Author liguiqin
     * @Date 2023/5/28 14:26
     * @param: param
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/28 14:26
     **/
    @GetMapping("/list")
    public Result list(OrdersPageRequest param, HttpServletRequest request) {
        Page<OrdersVO> resultPage = new Page<>();
        param.setUserId(sessionUtil.getUserId(request));//
        Page<OrdersBO> page = ordersService.page(param);
        List<OrdersVO> voList= warpList(page.getList());
        resultPage=new Page<>(page.getPageNum(),page.getPageSize(),page.getTotal(),voList);
        return Result.success(resultPage);
    }


    /*
     * @Description:查询订单详情
     * @Author liguiqin
     * @Date 2023/5/28 14:26
     * @param: param
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/28 14:26
     **/
    @GetMapping("/details")
    public Result details(Integer id, HttpServletRequest request) {
        if (id == null) {
            return Result.fail("ID不能为空");
        }
        Integer userId = sessionUtil.getUserId(request);//
        OrdersBO ordersBO = ordersService.getByIdAndUserId(id, userId);
        if (ordersBO == null) {
            return Result.fail("订单不存在");
        }
        List<OrdersVO> voList= warpList(Arrays.asList(ordersBO));
        return Result.success(voList.get(0));
    }

    /*
     * @Description:封装分页数据
     * @Author liguiqin
     * @Date 2023/5/29 9:05
     * @param: ordersBOS
     * @return com.nwcl.common.result.Page<com.nwcl.basic.vo.OrdersVO>
     * @UpdateDate 2023/5/29 9:05
     **/
    private List<OrdersVO> warpList(List<OrdersBO> ordersBOS) {
        List<OrdersVO> voList = new ArrayList<>();
        if (CollectionUtils.isEmpty(ordersBOS)) {
            return voList;
        }
        List<Integer> orderIds = new ArrayList<>();
        for (OrdersBO ordersBO : ordersBOS) {
            orderIds.add(ordersBO.getId());
        }
        Map<Integer, List<OrdersDetailBO>> ordersDetailMap = ordersDetailService.mapByOrderIds(orderIds);
        for (OrdersBO ordersBO : ordersBOS) {
            OrdersVO ordersVO = new OrdersVO();
            ordersVO.setOrderCode(ordersBO.getOrderCode());
            ordersVO.setId(ordersBO.getId());
            ordersVO.setRealPrice(ordersBO.getRealPrice());
            ordersVO.setPrice(ordersBO.getPrice());
            ordersVO.setCreateTime(DateUtil.formatDateTime(ordersBO.getCreateTime()));
            ordersVO.setStatus(ordersBO.getStatus());
            ordersVO.setStatusStr(Constant.OrderStatus.valMap.get(ordersBO.getStatus()));
            //设置订单详情
            for (OrdersDetailBO detailBO : ordersDetailMap.get(ordersBO.getId())) {
                OrdersGoodsVO ordersDetail = new OrdersGoodsVO();
                ordersDetail.setId(detailBO.getGoodsId());
                ordersDetail.setName(detailBO.getGoodsName());
                ordersDetail.setNum(detailBO.getNum());
                ordersDetail.setPrice(detailBO.getPrice());
                ordersDetail.setDefaultImg(detailBO.getGoodsImg());
                ordersVO.getGoodsList().add(ordersDetail);
            }
            voList.add(ordersVO);
        }
        return voList;
    }
}
